package JVM;

public class ThreadLocal_test {
    public static class thread1 implements Runnable{
        private ThreadLocal<String> local;

        public thread1(ThreadLocal<String> t){
            this.local=t;
        }
        @Override
        public void run() {
            local.set("a");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(local.get()+"---thread1");
        }
    }
    public static class thread2 implements Runnable{
        private ThreadLocal<String> local;

        public thread2(ThreadLocal<String> t){
            this.local=t;
        }
        @Override
        public void run() {
            local.set("b");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(local.get()+"----thread2");
        }
    }

    public static void main(String[] args) {
        ThreadLocal<String> t=new ThreadLocal<>();
        new Thread(new thread1(t)).start();
        new Thread(new thread2(t)).start();
    }
}
